View Javadoc
1   package org.apache.maven.surefire.common.junit4;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.report.ReportEntry;
23  import org.apache.maven.surefire.report.RunListener;
24  import org.apache.maven.surefire.report.SimpleReportEntry;
25  import org.apache.maven.surefire.report.StackTraceWriter;
26  import org.apache.maven.surefire.testset.TestSetFailedException;
27  import org.junit.runner.Description;
28  import org.junit.runner.Result;
29  import org.junit.runner.notification.Failure;
30  
31  import java.util.regex.Matcher;
32  import java.util.regex.Pattern;
33  
34  /**
35   * RunListener for JUnit4, delegates to our own RunListener
36   *
37   */
38  public class JUnit4RunListener
39      extends org.junit.runner.notification.RunListener
40  {
41      private static final Pattern PARENS = Pattern.compile( "^" + ".+" //any character
42                                                                 + "\\(("
43                                                                 // then an open-paren (start matching a group)
44                                                                 + "[^\\\\(\\\\)]+" //non-parens
45                                                                 + ")\\)" + "$" );
46  
47      protected final RunListener reporter;
48  
49      /**
50       * This flag is set after a failure has occurred so that a <code>testSucceeded</code> event is not fired.
51       * This is necessary because JUnit4 always fires a <code>testRunFinished</code> event-- even if there was a failure.
52       */
53      private final ThreadLocal<Boolean> failureFlag = new InheritableThreadLocal<Boolean>();
54  
55      private final JUnit4Reflector jUnit4Reflector = new JUnit4Reflector();
56  
57      /**
58       * Constructor.
59       *
60       * @param reporter the reporter to log testing events to
61       */
62      public JUnit4RunListener( RunListener reporter )
63      {
64          this.reporter = reporter;
65      }
66  
67      // Testrun methods are not invoked when using the runner
68  
69      /**
70       * Called when a specific test has been skipped (for whatever reason).
71       *
72       * @see org.junit.runner.notification.RunListener#testIgnored(org.junit.runner.Description)
73       */
74      public void testIgnored( Description description )
75          throws Exception
76      {
77          final String reason = jUnit4Reflector.getAnnotatedIgnoreValue( description );
78          final SimpleReportEntry report =
79              SimpleReportEntry.ignored( getClassName( description ), description.getDisplayName(), reason );
80          reporter.testSkipped( report );
81      }
82  
83      /**
84       * Called when a specific test has started.
85       *
86       * @see org.junit.runner.notification.RunListener#testStarted(org.junit.runner.Description)
87       */
88      public void testStarted( Description description )
89          throws Exception
90      {
91          reporter.testStarting( createReportEntry( description ) );
92          failureFlag.remove();
93      }
94  
95      /**
96       * Called when a specific test has failed.
97       *
98       * @see org.junit.runner.notification.RunListener#testFailure(org.junit.runner.notification.Failure)
99       */
100     @SuppressWarnings( { "ThrowableResultOfMethodCallIgnored" } )
101     public void testFailure( Failure failure )
102         throws Exception
103     {
104         String testHeader = failure.getTestHeader();
105         if ( isInsaneJunitNullString( testHeader ) )
106         {
107             testHeader = "Failure when constructing test";
108         }
109         ReportEntry report = SimpleReportEntry.withException( getClassName( failure.getDescription() ), testHeader,
110                                                               createStackTraceWriter( failure ) );
111 
112         if ( failure.getException() instanceof AssertionError )
113         {
114             this.reporter.testFailed( report );
115         }
116         else
117         {
118             this.reporter.testError( report );
119         }
120         failureFlag.set( Boolean.TRUE );
121     }
122 
123     protected StackTraceWriter createStackTraceWriter( Failure failure )
124     {
125         return new JUnit4StackTraceWriter( failure );
126     }
127 
128     @SuppressWarnings( { "UnusedDeclaration" } )
129     public void testAssumptionFailure( Failure failure )
130     {
131         this.reporter.testAssumptionFailure( createReportEntry( failure.getDescription() ) );
132         failureFlag.set( Boolean.TRUE );
133     }
134 
135 
136     /**
137      * Called after a specific test has finished.
138      *
139      * @see org.junit.runner.notification.RunListener#testFinished(org.junit.runner.Description)
140      */
141     public void testFinished( Description description )
142         throws Exception
143     {
144         Boolean failure = failureFlag.get();
145         if ( failure == null )
146         {
147             reporter.testSucceeded( createReportEntry( description ) );
148         }
149     }
150 
151     protected SimpleReportEntry createReportEntry( Description description )
152     {
153         return new SimpleReportEntry( getClassName( description ), description.getDisplayName() );
154     }
155 
156     public String getClassName( Description description )
157     {
158         String name = extractClassName( description );
159         if ( name == null || isInsaneJunitNullString( name ) )
160         {
161             // This can happen upon early failures (class instantiation error etc)
162             Description subDescription = description.getChildren().get( 0 );
163             if ( subDescription != null )
164             {
165                 name = extractClassName( subDescription );
166             }
167             if ( name == null )
168             {
169                 name = "Test Instantiation Error";
170             }
171         }
172         return name;
173     }
174 
175     private boolean isInsaneJunitNullString( String value )
176     {
177         return "null".equals( value );
178     }
179 
180     public static String extractClassName( Description description )
181     {
182         String displayName = description.getDisplayName();
183         Matcher m = PARENS.matcher( displayName );
184         if ( !m.find() )
185         {
186             return displayName;
187         }
188         return m.group( 1 );
189     }
190 
191     public static String extractMethodName( Description description )
192     {
193         String displayName = description.getDisplayName();
194         int i = displayName.indexOf( "(" );
195         if ( i >= 0 )
196         {
197             return displayName.substring( 0, i );
198         }
199         return displayName;
200     }
201 
202 
203     public static void rethrowAnyTestMechanismFailures( Result run )
204         throws TestSetFailedException
205     {
206         if ( run.getFailureCount() > 0 )
207         {
208             for ( Failure failure : run.getFailures() )
209             {
210                 if ( isFailureInsideJUnitItself( failure ) )
211                 {
212                     final Throwable exception = failure.getException();
213                     throw new TestSetFailedException( exception );
214                 }
215             }
216         }
217     }
218 
219     public static boolean isFailureInsideJUnitItself( Failure failure )
220     {
221         return failure.getDescription().getDisplayName().equals( "Test mechanism" );
222     }
223 }